<template>
  <div class="icons">
    <div class="swiper-container">
      <swiper ref="mySwiper" :options="swiperOption" :not-next-tick="notNextTick">
        <swiper-slide v-for="(page, index) of pages" :key="index">
          <div class="icon" v-for="item of page" :key="item.id">
            <div class="icon-img">
              <img class="icon-img-item" :src="item.iconUrl"/>
            </div>
            <p class="icon-text">{{item.text}}</p>
          </div>
        </swiper-slide>
        <!--
        <div class="swiper-button-prev" slot="button-prev"></div>
        <div class="swiper-button-next" slot="button-next"></div>
        -->
      </swiper>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HomeIcons',
  data () {
    return {
      iconUrlList: [
        {
          id: '01',
          text: '景点门票',
          iconUrl: require('images/icons/icon1.png')
        },
        {
          id: '02',
          text: '一日游',
          iconUrl: require('images/icons/icon2.png')
        },
        {
          id: '03',
          text: '上海迪士尼',
          iconUrl: require('images/icons/icon3.png')
        },
        {
          id: '04',
          text: '上海海昌',
          iconUrl: require('images/icons/icon4.png')
        },
        {
          id: '05',
          text: '野生动物园',
          iconUrl: require('images/icons/icon5.png')
        },
        {
          id: '06',
          text: '上海欢乐谷',
          iconUrl: require('images/icons/icon6.png')
        },
        {
          id: '07',
          text: '上影乐园',
          iconUrl: require('images/icons/icon7.png')
        },
        {
          id: '08',
          text: '东方明珠东方明珠东方明珠',
          iconUrl: require('images/icons/icon8.png')
        },
        {
          id: '09',
          text: '景点门票',
          iconUrl: require('images/icons/icon1.png')
        }
      ],
      notNextTick: true,
      swiperOption: {
        autoplay: false,
        speed: 500,
        loop: false,
        setWrapperSize: true,
        autoHeight: true,
        prevButton: '.swiper-button-prev',
        nextButton: '.swiper-button-next',
        mounsewheelControl: true, // 用鼠标 滑轮, 来控制滚动条的
        observeParents: true
      }
    }
  },
  computed: {
    swiper () {
      return this.$refs.mySwiper.swiper // 测试证明了,获取的swiper对象, 也就是jQuery包装的DOM对象
    },
    pages () {
      const pages = []
      this.iconUrlList.forEach((item, index) => {
        const page = Math.floor(index / 8) // 返回小于等于x的最大整数, (7 / 8)===> 0, 这里索引最大是8
        // 1.厉害,将pages[0] = [], 构建成二维数组
        if (!pages[page]) { // 而Math.round是四舍五入, 也就是大于等于
          pages[page] = []
        }
        pages[page].push(item) // 1.厉害, 如何构建二维数组 pages[0] = []
      })
      return pages
    }
  },
  mounted () {
    // console.log(this.$refs)
    // console.log(this.pages)
    // console.log(this.swiper)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  @import '~styles/mixins.styl'
  .icons >>> .swiper-container
    height: 0
    padding-bottom: 50% /*1.表示高度是 自身的 宽度的一半, 宽度100%,默认屏幕是1000px */
  .icons
    margin-top: .1rem
    /*1.一个图标 + 文字的整体尺寸 */
    .icon
      position: relative
      overflow: hidden
      float: left
      width: 25% /*2.表示宽度是 父容器 宽度的25%, 也就是父容器 高度的一半 */
      /*等于宽度是整个icons容器的高度的一半,比较绕口, 没实际意义*/
      height: 0
      padding-bottom: 25% /*3.主要表示图片的尺寸, 更重要的是图片加载完成前的 占位符? 理解万岁 */
      .icon-img
        position: absolute
        top: 0
        left: 0
        right: 0
        bottom: .44rem
        box-sizing: border-box /*让padding撑开,好让四周有空隙*/
        padding: .05rem
        /*记住倍数关系,所以实际像素是2.5px
        1rem = 50px
        0.05rem = 2.5px
        .4rem = 20px
        简单来说,一半rem都用 .多少表示, 而像素一半都是10px, 20px, 整倍数
        */
        .icon-img-item
          display: block
          margin: 0 auto
          height: 100%
      .icon-text
        position: absolute
        left: 0
        right: 0
        bottom: 0
        height: .44rem
        line-height: .44rem
        text-align: center
        color: $darkTextColor
        /*
        overflow: hidden
        white-space: nowrap
        text-overflow: ellipsis
        */
        ellipsis()
</style>
